//
//  HOPeakValleyMeterViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/30/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOPeakValleyMeterViewController.h"
#import "HOMeterListViewController.h"
#import "HOConstants.h"
#import "HOUtils.h"

@interface HOPeakValleyMeterViewController ()
{

}
@property(nonatomic, retain)	UITextField *noInputBox;  //峰
@property(nonatomic, retain)	UITextField *peakInputBox;  //峰
@property(nonatomic, retain)	UITextField *avgInputBox; //谷
@property(nonatomic, retain)	UITextField *pointInputBox; //平均
@property(nonatomic, retain)	UITextField *vallyInputBox;//尖

@property(nonatomic, retain)	UITextField *maxRequiredInputBox;//最大需求
@property(nonatomic, retain)	UITextField *inactivityInputBox; //无功率电量
@property(nonatomic, retain)	UITextField *totalInputBox;//总量
@property(nonatomic, assign)double createTime;
@property(nonatomic, assign)int meterId;
@property(nonatomic, assign)int meterValue;
@end

@implementation HOPeakValleyMeterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self initSubviews];
	[self registerKeyboard];
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_LastTimeMeters,
								  kHO_FIELD_MeterType:[NSNumber numberWithInt:1]}];
}

- (void)initSubviews
{
	self.noInputBox = [self getInputBox:NSLocalizedString(@"请输入表编号", @"请输入表编号")];
	self.peakInputBox = [self getInputBox:NSLocalizedString(@"请输入峰电量", @"请输入峰电量")];
	self.pointInputBox = [self getInputBox:NSLocalizedString(@"请输入尖电量", @"请输入尖电量")];
	self.avgInputBox = [self getInputBox:NSLocalizedString(@"请输入平电量", @"请输入平电量")];
	self.vallyInputBox = [self getInputBox:NSLocalizedString(@"请输入谷电量", @"请输入谷电量")];
	self.maxRequiredInputBox = [self getInputBox:NSLocalizedString(@"请输最大需量", @"请输最大需量")];
	self.inactivityInputBox = [self getInputBox:NSLocalizedString(@"请输入无功电量", @"请输入无功电量")];
	self.totalInputBox = [self getInputBox:NSLocalizedString(@"请输入总电量", @"请输入总电量")];
	
	baseTableview.tableFooterView = [self getFooterViewWithTitle:NSLocalizedString(@"完成抄表", @"完成抄表")];
}

- (void)hideKeyboard
{
	[self.noInputBox resignFirstResponder];
	[self.peakInputBox resignFirstResponder];
	[self.pointInputBox resignFirstResponder];
	[self.avgInputBox resignFirstResponder];
	[self.vallyInputBox resignFirstResponder];
	[self.maxRequiredInputBox resignFirstResponder];
	[self.inactivityInputBox resignFirstResponder];
	[self.totalInputBox resignFirstResponder];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (UITextField *)getInputBox:(NSString *)pleaceHolder
{
	UITextField *inputBox = [[[UITextField alloc]init] autorelease];
	inputBox.frame = CGRectMake(80,
								(CELL_HEIGHT - 22) / 2,
								self.view.frame.size.width - 80 - HO_HORIZONAL_PADDING,
								22);
	inputBox.placeholder = pleaceHolder;
	inputBox.textColor = TEXT_COLOR_999999;
	inputBox.font = [UIFont systemFontOfSize:15];
	inputBox.keyboardType = UIKeyboardTypeNumberPad;
	inputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
	return inputBox;
}

- (void)didClickFooterButton:(UIButton *)btn
{
	
	[self hideKeyboard];
	NSString *noValue = [self getTrimingCharaFromTextField:self.noInputBox];
	NSString *peakValue = [self getTrimingCharaFromTextField:self.peakInputBox];
	NSString *avgValue = [self getTrimingCharaFromTextField:self.avgInputBox];
	NSString *pointValue = [self getTrimingCharaFromTextField:self.pointInputBox];
	NSString *vallyValue = [self getTrimingCharaFromTextField:self.vallyInputBox];
	NSString *maxValue = [self getTrimingCharaFromTextField:self.maxRequiredInputBox];
	NSString *inactivityValue = [self getTrimingCharaFromTextField:self.inactivityInputBox];
	NSString *totalValue = [self getTrimingCharaFromTextField:self.totalInputBox];
	if(noValue.length == 0)
	{
		[self popupHintView:self.noInputBox.placeholder];
		return;
	}
	if(peakValue.length == 0)
	{
		[self popupHintView:self.peakInputBox.placeholder];
		return;
	}
	if(pointValue.length == 0)
	{
		[self popupHintView:self.pointInputBox.placeholder];
		return;
	}
	if(avgValue.length == 0)
	{
		[self popupHintView:self.avgInputBox.placeholder];
		return;
	}
	if(vallyValue.length == 0)
	{
		[self popupHintView:self.vallyInputBox.placeholder];
		return;
	}
	if(maxValue.length == 0)
	{
		[self popupHintView:self.maxRequiredInputBox.placeholder];
		return;
	}
	if(inactivityValue.length == 0)
	{
		[self popupHintView:self.inactivityInputBox.placeholder];
		return;
	}
	if(totalValue.length == 0)
	{
		[self popupHintView:self.totalInputBox.placeholder];
		return;
	}
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_SubmitMetersValue,
								  @"UnEffective":inactivityValue,
								  @"MaxRequire":maxValue,
								  @"MeterNum":noValue,
								  @"VallyValue":vallyValue,
								  @"PointValue":pointValue,
								  @"AverageValue":avgValue,
								  @"PeakValue":peakValue,
								  @"MeterValue":@"12",
								  kHO_FIELD_MeterType:[NSNumber numberWithInt:1]}];
	
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"峰谷电表", @"峰谷电表")];
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"抄表列表", @"抄表列表")];
}

- (void)rightButtonItemDidClick
{
	HOMeterListViewController *viewCtrl = [[HOMeterListViewController alloc] init];
	[self.navigationController pushViewController:viewCtrl animated:YES];
	[viewCtrl release];
}

- (void)dealloc
{
	self.peakInputBox = nil;
	self.avgInputBox = nil;
	self.pointInputBox = nil;
	self.vallyInputBox = nil;
	self.maxRequiredInputBox = nil;
	self.inactivityInputBox = nil;
	self.totalInputBox = nil;
	[super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 5;
	else if(section == 1)
		return 3;
    return 2;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
    }
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.textLabel.font = [UIFont systemFontOfSize:15];
	cell.textLabel.textColor = TEXT_COLOR_666666;
	
	if(indexPath.section == 0)
	{
		if(indexPath.row == 0)
		{
			cell.textLabel.text = NSLocalizedString(@"表编号", @"表编号");
			if((UITextField *)[cell viewWithTag:1132] == nil)
			{
				self.noInputBox.tag = 1132;
				[cell.contentView addSubview:self.noInputBox];
			}
		}
		else if(indexPath.row == 1)
		{
			cell.textLabel.text = NSLocalizedString(@"峰电量", @"峰电量");
			if((UITextField *)[cell viewWithTag:112] == nil)
			{
				self.peakInputBox.tag = 112;
				[cell.contentView addSubview:self.peakInputBox];
			}
		}
		else if(indexPath.row == 2)
		{
			cell.textLabel.text = NSLocalizedString(@"平电量", @"平电量");
			if((UITextField *)[cell viewWithTag:113] == nil)
			{
				self.pointInputBox.tag = 113;
				[cell.contentView addSubview:self.avgInputBox];
			}
		}
		else if(indexPath.row == 3)
		{
			cell.textLabel.text = NSLocalizedString(@"尖电量", @"尖电量");
			if((UITextField *)[cell viewWithTag:114] == nil)
			{
				self.vallyInputBox.tag = 114;
				[cell.contentView addSubview:self.pointInputBox];
			}
		}
		else if(indexPath.row == 4)
		{
			cell.textLabel.text = NSLocalizedString(@"谷电量", @"谷电量");
			if((UITextField *)[cell viewWithTag:115] == nil)
			{
				self.avgInputBox.tag = 115;
				[cell.contentView addSubview:self.vallyInputBox];
			}
		}
	}
	else if(indexPath.section == 1)
	{
		if(indexPath.row == 0)
		{
			cell.textLabel.text = NSLocalizedString(@"最大需量", @"最大需量");
			if((UITextField *)[cell viewWithTag:116] == nil)
			{
				self.maxRequiredInputBox.tag = 116;
				[cell.contentView addSubview:self.maxRequiredInputBox];
			}
		}
		else if(indexPath.row == 1)
		{
			cell.textLabel.text = NSLocalizedString(@"无功电量", @"无功电量");
			if((UITextField *)[cell viewWithTag:117] == nil)
			{
				self.inactivityInputBox.tag = 117;
				[cell.contentView addSubview:self.inactivityInputBox];
			}
		}
		else if(indexPath.row == 2)
		{
			cell.textLabel.text = NSLocalizedString(@"总电量", @"总电量");
			if((UITextField *)[cell viewWithTag:118] == nil)
			{
				self.totalInputBox.tag = 118;
				[cell.contentView addSubview:self.totalInputBox];
			}
		}
	}
	else if(indexPath.section == 2)
	{
		if(indexPath.row == 0)
		{
			NSString *content = [NSString stringWithFormat: @"上期总电量: %d", self.meterValue];
			cell.textLabel.text = NSLocalizedString(content, content);
		}
		else if(indexPath.row == 1)
		{
			NSString *content = [NSString stringWithFormat: @"日期: %@", [HOUtils getDateFromTimeInterval4:self.createTime * 1000]];
			cell.textLabel.text = NSLocalizedString(content, content);
		}
	}
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
#pragma mark -- tool

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_LastTimeMeters])
	{
		self.createTime = [[response.dataNode objectForKey:kHO_FIELD_CreateTime] doubleValue];
		self.meterId = [[response.dataNode objectForKey:kHO_FIELD_MeterId] intValue];
		self.meterValue = [[response.dataNode objectForKey:kHO_FIELD_MeterValue] intValue];
		[baseTableview reloadData];
	}
	else if([response isEqualAction:kHO_ACTION_SubmitMetersValue])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:NSLocalizedString(@"提交成功", @"提交成功") inTime:1.5 needCallback:YES];
	}
}

- (void)hideHintView
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
